package com.vanvalt.payhui.modules.pay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.vanvalt.payhui.modules.pay.entity.Pos;
import com.vanvalt.payhui.modules.pay.service.PosService;

/**
 * @author Xiaoyang
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/pos")
public class PosController extends BaseController {

	@Autowired
	private PosService posService;
	
	@ModelAttribute
	public Pos get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return posService.get(id);
		}else{
			return new Pos();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Pos pos, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Pos> page = posService.findPage(new Page<Pos>(request, response), pos); 
        model.addAttribute("page", page);
		return "modules/pay/posList";
	}
	
	@RequestMapping(value = "form")
	public String form(Pos pos, Model model) {
		
		model.addAttribute("pos", pos);
		return "modules/pay/posForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Pos pos, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pos)){
			return form(pos, model);
		}
		posService.save(pos);
		addMessage(redirectAttributes, "保存POS信息'" + StringUtils.abbr(pos.getName(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/pos/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Pos pos, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		posService.delete(pos);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"POS信息成功");
		return "redirect:" + adminPath + "/pay/pos/?repage";
	}
	
}
